package com.xianbeiying.app.ckgl.model;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.bean.JsonBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpPostManger;
import com.xianbeiying.app.ckgl.util.DialogUtile;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;

import cz.msebera.android.httpclient.Header;

/**
 * 登录网络访问
 */
public class LoginModel extends BaseModel{

    private AsyncHttpClient client;
    private RequestParams params;
    private Context context;
    public LoginModel(Context context){
        this.context=context;
        client = new AsyncHttpClient();
        params = new RequestParams();
//        MyApplication.IP= SharedPreferencesUtils.getStringDate("ip","120.26.96.123");
    }

    /**
     *
     * @param username 用户名
     * @param password 密码
     * @param isDiaylog
     */
    public  void getLoginDatas(final String username, final String password,boolean isDiaylog){
        if(isDiaylog){
            DialogUtile.showLoading(context);
        }
        LogUtils.e("url",UrlBean.LOGIN+params+JsonBean.loginjsonString(username,password));
       HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.LOGIN, JsonBean.loginjsonString(username,password), new AsyncHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            //关闭加载
            DialogUtile.closeDialog();
             Log.e("返回值",new String(responseBody));
            //函数返回值
            LoginModel.this.onCallBackData(UrlBean.LOGIN,responseBody);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            //关闭加载
            DialogUtile.closeDialog();
            LogUtils.e("返回值", "----------------"+statusCode);
            if(statusCode==0){
                ToastUtil.showInfo(context,"无法连接平台");
            }else if(statusCode==302){
                MyApplication.isLogin=false;
//                    ToastUtil.showInfo(context,"请先登录");
            }else{
                ToastUtil.showInfo(context,"无法连接平台");
            }
        }
    });
}
    /**
     * 退出登录
     * */
    public  void getLogout(boolean isDiaylog){
        if(isDiaylog){
            DialogUtile.showLoading(context);
        }
        LogUtils.e("url","http://"+MyApplication.IP+UrlBean.LOGOUT);
        HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.LOGOUT,"", new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
                DialogUtile.closeDialog();
                Log.e("返回值",new String(responseBody));
                //函数返回值
                LoginModel.this.onCallBackData("退出",responseBody);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                LogUtils.e("返回值", "----------------"+statusCode);
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else if(statusCode==302){
                    MyApplication.isLogin=false;
//                    ToastUtil.showInfo(context,"请先登录");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });
    }
}
